import json
import time
from openai import OpenAI
from openai.types.chat import ChatCompletionUserMessageParam,ChatCompletionSystemMessageParam
from pydantic import BaseModel
# from extensions.ext_database import db

class BaseMessage(BaseModel):
    content:str
class SystemMessage(BaseMessage):
    role:str="system"
class UserMessage(BaseMessage):
    role:str="user"

client = OpenAI(api_key="sk-351257cfe69d4d7890c420cae8897f3d",
                 base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",)
# ChatCompletionMessageParam(content="", role="system")
begin = time.perf_counter()*1000
tools = [
    {
        "name": "increase_volume",
        "description": "当你需要调高设备音量时，请使用此工具。",
        # "parameters": {
        #     "type": "object",
        #     "properties": {
        #         "volume": {
        #             "type": "integer",
        #             "nullable": True,
        #             "description": "音量，取值范围0-100,可以为空值,表示按照默认音量调高，没有目标音量",
        #         }
        #     }
        # },

    },
    {
        "name": "set_volume",
        "description": "当你需要设置设备音量到特定值时，请使用此工具。",
        "parameters": {
            "type": "object",
            "properties": {
                "volume": {
                    "type": "integer",
                    "nullable": True,
                    "description": "音量，取值范围0-100,",
                }
            }
        },

    },
    {
        "name": "decrease_volume",
        "description": "当你需要调低设备音量时，请使用此工具。",
    },
    {
        "name": "get_current_weather",
        "description": "当你想查询指定城市的天气时非常有用。",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "nullable": True,
                    "description": "城市或县区,可以为空。",
                }
            },
            # "required": ["location"]
        }
    }
]
tools_string = json.dumps(tools,ensure_ascii=False)
system_prompt = f"""You are Qwen, created by Alibaba Cloud. You are a helpful assistant. You may call one or more tools to assist with the user query. The tools you can use are as follows:
{tools_string}
Response in INTENT_MODE."""


completion = client.chat.completions.create(
    model="tongyi-intent-detect-v3",#"qwen3-235b-a22b",
    messages=[
        ChatCompletionSystemMessageParam(role="system",content=system_prompt),
        ChatCompletionUserMessageParam(role="user",content="把声音调到最大"),       
    ],
    # stream=True,
    # stream_options={"inlcude_usage": True}
)
print(completion.choices[0].message.content)
print("time: %.3f ms" % (time.perf_counter()*1000-begin))
# if __name__ == "__main__":
    # print(WeatherService.get_city_code("上海"))#101020100
    # print(WeatherService.get_weather_by_24hours("101020100"))
    # code_map = WeatherService.get_location_code_map()
    # print(WeatherService.get_weather_by_postal("上海"))
